//
//  When.swift
//  ObserveTiming
//
//  Created by JY_NEW on 2021/10/30.
//

import Foundation

/// 指定观察者
public func when<T>(_ ob: T) -> Observer<T> {
    return Observer<T>(ob: ob)
}

/// 指定观察时机
public func when(_ timing: ObserveTiming) -> ObserveRouter<Void> {
    return ObserveRouter<Void>(ob: (), timing: timing)
}

class Example {
    var myTime = ObserveTiming()
    func xxxx() {
        // KVO
        let target = UILabel()
        when(self).myTime.observe(target).text.before { example,  value in
            
        }
        when(self).myTime.unobserve(target).text.before()
        // 黑魔法
        when(self).myTime.observe(target).method(#selector(UIView.layoutSubviews)).before { example, params in
            
        }
        when(self).myTime.unobserve(target).method(#selector(UIView.layoutSubviews)).before()
        // Delegate
        let scroll = UIScrollView()
        when(self).myTime.observe(scroll).delegate
            .method(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
            .before { example, params in
            
        }
        when(self).myTime.unobserve(scroll).delegate
            .method(#selector(UIScrollViewDelegate.scrollViewDidScroll(_:)))
            .before()
        // NotificationCenter
        when(self).myTime.observe(.UIApplicationDidBecomeActive) { example, userInfo in
            
        }
        when(self).myTime.unobserve(.UIApplicationDidBecomeActive)
        when(self).myTime.observe("name of notification") { example, userInfo in
            
        }
        when(self).myTime.unobserve("name of notification")
        when(self).myTime.once(.UIApplicationDidBecomeActive) { example, userInfo in
            
        }
    }
}

public protocol FXEventType {
    associatedtype ParameterType
    func addTarget(_ target: Any, action: Selector)
    func removeTarget(_ target: Any, action: Selector)
    func removeTarget(_ target: Any)
    func sendActions()
}
/// KVO事件
public class FXKvoEvent: FXEventType {
    
}
/// 消息通知事件
public class FXNotificationEvent: FXEventType {
    
}
/// 方法注入事件
public class FXMethodEvent: FXEventType {
    
}
/// 委托注入事件
public class FXDelegateEvent: FXEventType {
    
}
/// 普通事件
public class FXEvent: FXEventType {
    
}
